package org.mspring.mlog.web.support.log;

import java.util.HashMap;
import java.util.Map;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.mspring.platform.config.ConfigService;
import org.mspring.platform.security.SecurityUtils;
import org.mspring.platform.web.WebContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 *
 * @author Gao Youbo
 * @since 2013-3-11
 */
@Aspect
@Component
public class LogAspect {

    @Autowired
    private LogTask logTask;
    @Autowired
    private ConfigService configService;

    @AfterReturning("within(org.mspring.mlog..*) && @annotation(logAnno)")
    public void log(final JoinPoint jp, final Log logAnno) throws Exception {
        String syslog = configService.getConfig("system_log");
        if (!"true".equals(syslog)) { // 关闭系统日志
            return;
        }
        Map<Object, Object> context = new HashMap<>();
        context.put("request", WebContext.getInstance().getRequest());
        context.put("joinPoint", jp);
        context.put("logAnno", logAnno);
        context.put("user", SecurityUtils.getCurrentUser());
        logTask.doAsyncTask(context);
    }
}
